#include "Snake.h"
#include "EcoSystem.h"
#include "Environment.h"
#include<iostream>

Snake::Snake(EcoSystem *eco_system) : Consumer(eco_system)
{
    set_alive();
    set_max_hp(12000);
    set_hp(get_max_hp()/2);
    set_init_speed(10.0);
    set_speed(get_init_speed());
}
Snake::~Snake() {}

const unsigned int Snake::get_species_id() const
{
    return SNAKE_SPECIES_ID;
}

const unsigned int Snake::get_life_span()
{
    return 0x3f3f3f3f;
}

Gdiplus::Image *Snake::get_entity_image() const
{
    static Gdiplus::Image *img = Gdiplus::Image::FromFile(L".\\images\\snake_l.png");
    static Gdiplus::Image *img_r = Gdiplus::Image::FromFile(L".\\images\\snake_r.png");
    static Gdiplus::Image *img_s = Gdiplus::Image::FromFile(L".\\images\\snake_s.png");

    if (is_sleep)
        return img_s;
    else if ((get_position() - get_prev_position()).x < 0)
        return img;
    else
        return img_r;
}

Entity *Snake::new_entity() const
{
    return new Snake(eco_system);
}

void Snake::on_tick()
{
    Environment *ptr = eco_system->get_environment_instance();
    if (ptr->get_temperature() > 30.0 || ptr->get_temperature() < 5.0){
        set_hp(get_hp()*0.8);
    }

    if (ptr->get_season() == 3){  //winter
        set_init_speed(0.0);
        is_sleep = true;
    }
    else{
        set_age(get_age() + 1);

        if (get_age() > get_life_span())
            set_dead();

        set_hp(get_hp() - get_speed() * get_speed() / 2);
        if (get_hp() > get_max_hp()*0.8 && EcoSystem::random_double() < 0.3 && get_age() < get_life_span()*0.6)
        {
            multiply();
            set_hp(get_hp()*0.75);
        }
        if (!avoid_predator(get_init_speed()*1.5))
        if (!prey(get_init_speed()))
            random_motion(get_init_speed()*0.5);

        /*if (go_left())
            is_go_left = true;
        else
            is_go_left = false;*/

    }
}

int    Snake::get_cost_of_being_preyed() const
{
    return get_hp() * 0.15;
}
int    Snake::get_gain_after_being_preyed() const
{
    return get_hp() * 0.2;
}